package com.cip.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 * 
 * @author ranjeeth
 *
 */
public class GridUtil {

	public static String templateName = "GridTemplate.vm";
	
	public static String generateScript(VelocityEngine velocityEngine, Map<String, Object> context)throws Exception{
		  String result = null;
	        try {
	        	if(null == context.get("noOfRecords")){
	        		context.put("noOfRecords", "10");
	        	}
	        	if(null == context.get("link")){
	        		context.put("link", "");
	        	}
	        	
	        	System.out.println("context============="+context);
	        	
	            result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, context);
	        } catch (Exception e) {
	           throw new Exception("Error while generating script using velocity",e);
	        }
		return result;
	}
	

	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> instArrayList, Map<String, Object> context)
			throws Exception {
		String jsonFieldValues = "";
		try {
			if (instArrayList != null && !(instArrayList.isEmpty())) {
				jsonFieldValues = CommonUtil.getJsonString(instArrayList);
			}
		} catch (Exception e) {
			throw new Exception("Problem parsing processDefinitions as json", e);
		}
		
		System.out.println("jsonFieldValues============="+jsonFieldValues);
		
		context.put("jsonFieldValues", jsonFieldValues);
		
		System.out.println("generateScriptForProcess========context=="+context);
		
		return generateScript(velocityEngine, context);
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForRoleGrid(List<Map<String,Object>> list, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','Role Name','Description','Type','Created At']";		
		context.put("title", "Roles");
		context.put("needCheckbox", true);
		/*context.put("link", "/bpm/admin/newRole");*/
		context.put("gridId", "ROLE_LIST");
		context.put("noOfRecords", "20");
		String jsonFieldValues = "";
		if(list != null && !(list.isEmpty())){
			jsonFieldValues = CommonUtil.getJsonString(list);	
		}
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "name", "100", "left", "_showEditRole", "false");
		CommonUtil.createFieldNameList(fieldNameList, "description", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "roleType", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "createdTimeByString", "100", "left", "", "false");
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForGroupGrid(List<Map<String,Object>> list, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','Group Name','Description','Person Incharge','Created At']";		
		context.put("title", "Groups");
		context.put("gridId", "GROUP_LIST");
		//context.put("link", "bpm/admin/groupForm");
		context.put("noOfRecords", "20");
		context.put("needCheckbox", true);
		String jsonFieldValues = "";
		if(list != null && !(list.isEmpty())){
			jsonFieldValues = CommonUtil.getJsonString(list);	
		}
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "name", "100", "left", "_showEditGroup", "false");
		CommonUtil.createFieldNameList(fieldNameList, "description", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "personIncharge", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "createdTimeByString", "100", "left", "", "false");
		
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForDepartmentGrid(List<Map<String,Object>> list, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','Department Name','Description','Department Owner','Created At','Department Type']";		
		context.put("title", "Departments");
		context.put("gridId", "DEPARTMENT_LIST");
		/*context.put("link", "bpm/admin/departmentForm");*/
		context.put("noOfRecords", "20");
		context.put("needCheckbox", true);
		String jsonFieldValues = "";
		if(list != null && !(list.isEmpty())){
			jsonFieldValues = CommonUtil.getJsonString(list);	
		}
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "name", "100", "left", "_showEditDepartment", "false");
		CommonUtil.createFieldNameList(fieldNameList, "description", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "departmentOwner", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "createdTimeByString", "100", "center", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "departmentType", "100", "center", "", "true");
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForMenuGrid(List<Map<String,Object>> list, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','Menu Name','Description']";		
		context.put("title", "Menus");
		context.put("gridId", "MENU_LIST");
		context.put("link", "bpm/admin/menuForm");
		context.put("noOfRecords", "20");
		String jsonFieldValues = "";
		if(list != null && !(list.isEmpty())){
			jsonFieldValues = CommonUtil.getJsonString(list);	
		}
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "name", "100", "left", "_showEditMenu", "false");
		CommonUtil.createFieldNameList(fieldNameList, "description", "100", "left", "", "false");
		
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForUserGrid(List<Map<String,Object>> list, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','User Name','Full Name','Department','Email','Employee Number','Status']";		
		context.put("title", "Users");
		context.put("needCheckbox", true);
		context.put("gridId", "USER_LIST");
		/*context.put("link", "/bpm/admin/userform");
		context.put("params", "method=add");*/
		context.put("noOfRecords", "20");
		context.put("dynamicGridWidth", "organizationUserGridWidth");
		context.put("dynamicGridHeight", "organizationGridHeight");
		String jsonFieldValues = "";
		if(list != null && !(list.isEmpty())){
			jsonFieldValues = CommonUtil.getJsonString(list);	
		}
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "50", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "username", "100", "left", "_showEditUser", "false");
		CommonUtil.createFieldNameList(fieldNameList, "fullName", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "department", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "email", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "employeeNumber", "80", "center", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "enabled", "50", "center", "_showEnabled", "false");
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForDataDictionaryGrid(List<Map<String,Object>> dictionaryList, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','Dictionary Name','Dictionary Code','Status','Inferior Dictionary']";		
		context.put("title", "Data Dictionary List");
		context.put("gridId", "DICTIONARY_LIST");
		
		String jsonFieldValues = "[{'id':'123','dictionaryName':'','dictionaryCode':'','status':'','inferiorDictionary':''}]";
		
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "dictionaryName", "150", "left", "","false");
		CommonUtil.createFieldNameList(fieldNameList, "dictionaryCode", "150", "left", "","false");
		CommonUtil.createFieldNameList(fieldNameList, "status", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "inferiorDictionary", "100", "left", "", "false");
		
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForScheduleGrid(List<Map<String,Object>> list, VelocityEngine velocityEngine) throws Exception{
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['Id','Event Name','Description','Event Type ','Location','Start Date','End Date','Start Time','End Time','Assigned']";		
		context.put("title", "Schedule");
		context.put("gridId", "SCHEDULE_LIST");
		context.put("noOfRecords", "20");
		context.put("needCheckbox", true);
		String jsonFieldValues = "";
		if(list != null && !(list.isEmpty())){
			jsonFieldValues = CommonUtil.getJsonString(list);	
		}
		context.put("jsonFieldValues", jsonFieldValues);
		context.put("columnNames", columnNames);	
		CommonUtil.createFieldNameList(fieldNameList, "id", "100", "left", "", "true");
		CommonUtil.createFieldNameList(fieldNameList, "eventName", "100", "left", "_showEditSchedule", "false");
		CommonUtil.createFieldNameList(fieldNameList, "description", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "eventType", "100", "left", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "location", "100", "center", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "startDate", "100", "center", "_changeStartDateFormat", "false");
		CommonUtil.createFieldNameList(fieldNameList, "endDate", "100", "center", "_changeEndDateFormat", "false");
		CommonUtil.createFieldNameList(fieldNameList, "startTime", "100", "center", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "endTime", "100", "center", "", "false");
		CommonUtil.createFieldNameList(fieldNameList, "assignedUser", "100", "center", "", "false");
		context.put("fieldNameList", fieldNameList);		
		return GridUtil.generateScript(velocityEngine, context);
	}
}
